package com.easylinkin.linkappapi.airconditioner.util;

/**
 * class info :定时Cron表达式生成工具
 *
 * @author liuqihang
 * @date 2021/5/11 11:21
 */
public class CronExpressionUtil {

    /**
     *
     * @param startTime 开始时间，示例：2021-05-11
     * @param endTime   结束时间，示例：2021-05-15
     * @param executeTime   执行时间，示例：08:22:03
     * @return  示例：cron:"03 22 08 11-15 5 ? 2021-2021" 或者""03 22 08 11-15 5-5 ? 2021-2021""
     * 描述：2021年5月11~15号 每天08:22:03点执行
     */
    public static String generateAirCron(String startTime, String endTime, String executeTime){
        StringBuilder sb = new StringBuilder();
        //生成执行时间段字符串
        String ss = executeTime.substring(6, 8);
        sb.append(ss).append(" ");
        String mm = executeTime.substring(3, 5);
        sb.append(mm).append(" ");
        String hh = executeTime.substring(0, 2);
        sb.append(hh).append(" ");

        String startDay = startTime.substring(8, 10);
        String endDay = endTime.substring(8, 10);
        sb.append(startDay).append("-").append(endDay).append(" ");

        String startMonth = startTime.substring(5, 7);
        String endMonth = endTime.substring(5, 7);
        if(Integer.valueOf(startMonth) < 11){
            startMonth = startTime.substring(6, 7);
        }
        if(Integer.valueOf(endMonth) < 11){
            endMonth = endTime.substring(6, 7);
        }
        sb.append(startMonth).append("-").append(endMonth).append(" ? ");

        String startYear = startTime.substring(0, 4);
        String endYear = endTime.substring(0, 4);
        sb.append(startYear).append("-").append(endYear);
        return sb.toString();
    }
}
